<script setup>
import { ref } from 'vue'

// 场景列表
const scenes = ref([
  {
    id: 1,
    name: '晨间音乐',
    icon: '🌅',
    active: false,
    schedule: '每天 07:00',
    devices: ['客厅音箱', '智能灯带'],
    actions: [
      { device: '客厅音箱', action: '播放晨间歌单' },
      { device: '智能灯带', action: '渐亮至80%' }
    ]
  },
  {
    id: 2,
    name: '睡眠模式',
    icon: '🌙',
    active: true,
    schedule: '每天 23:00',
    devices: ['卧室音箱', '智能灯带'],
    actions: [
      { device: '卧室音箱', action: '播放助眠白噪音' },
      { device: '智能灯带', action: '渐暗至关闭' }
    ]
  },
  {
    id: 3,
    name: '回家模式',
    icon: '🏠',
    active: false,
    schedule: '位置触发',
    devices: ['空气净化器', '智能灯带', '客厅音箱'],
    actions: [
      { device: '空气净化器', action: '开启强力模式' },
      { device: '智能灯带', action: '开启温暖色调' },
      { device: '客厅音箱', action: '播放轻音乐' }
    ]
  }
])

// 定时任务
const schedules = ref([
  {
    id: 1,
    time: '07:00',
    days: ['周一', '周二', '周三', '周四', '周五'],
    scene: '晨间音乐',
    enabled: true
  },
  {
    id: 2,
    time: '23:00',
    days: ['每天'],
    scene: '睡眠模式',
    enabled: true
  }
])

// 联动规则
const rules = ref([
  {
    id: 1,
    name: '空气质量联动',
    condition: '空气质量差',
    actions: ['开启空气净化器'],
    enabled: true
  },
  {
    id: 2,
    name: '音乐停止联动',
    condition: '音乐播放结束',
    actions: ['关闭氛围灯光'],
    enabled: false
  }
])

// 切换场景状态
const toggleScene = (scene) => {
  scene.active = !scene.active
}

// 切换定时任务状态
const toggleSchedule = (schedule) => {
  schedule.enabled = !schedule.enabled
}

// 切换联动规则状态
const toggleRule = (rule) => {
  rule.enabled = !rule.enabled
}

// 添加场景
const showAddScene = ref(false)
const toggleAddScene = () => {
  showAddScene.value = !showAddScene.value
}
</script>

<template>
  <div class="scenes-page">
    <!-- 场景列表 -->
    <div class="scenes-section">
      <div class="section-header">
        <h2>智能场景</h2>
        <button 
          class="add-btn"
          @click="toggleAddScene"
        >
          新建场景
        </button>
      </div>

      <div class="scenes-grid">
        <div 
          v-for="scene in scenes"
          :key="scene.id"
          class="scene-card"
        >
          <div class="card-header">
            <span class="scene-icon">{{ scene.icon }}</span>
            <div class="scene-info">
              <h3>{{ scene.name }}</h3>
              <span class="schedule-tag">{{ scene.schedule }}</span>
            </div>
            <button 
              class="toggle-btn"
              :class="{ active: scene.active }"
              @click="toggleScene(scene)"
            >
              {{ scene.active ? '已启用' : '已关闭' }}
            </button>
          </div>

          <div class="devices-list">
            <div 
              v-for="(device, index) in scene.devices"
              :key="index"
              class="device-tag"
            >
              {{ device }}
            </div>
          </div>

          <div class="actions-list">
            <div 
              v-for="(action, index) in scene.actions"
              :key="index"
              class="action-item"
            >
              <span class="device-name">{{ action.device }}</span>
              <span class="action-desc">{{ action.action }}</span>
            </div>
          </div>

          <div class="card-actions">
            <button class="action-btn edit">
              编辑
            </button>
            <button class="action-btn delete">
              删除
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 定时任务 -->
    <div class="schedules-section">
      <h2>定时任务</h2>
      <div class="schedules-list">
        <div 
          v-for="schedule in schedules"
          :key="schedule.id"
          class="schedule-item"
        >
          <div class="schedule-info">
            <div class="time">{{ schedule.time }}</div>
            <div class="days">
              {{ schedule.days.join(' ') }}
            </div>
            <div class="scene-name">
              执行: {{ schedule.scene }}
            </div>
          </div>
          <label class="switch">
            <input
              :checked="schedule.enabled"
              type="checkbox"
              @change="toggleSchedule(schedule)"
            >
            <span class="slider"></span>
          </label>
        </div>
      </div>
    </div>

    <!-- 联动规则 -->
    <div class="rules-section">
      <h2>联动规则</h2>
      <div class="rules-list">
        <div 
          v-for="rule in rules"
          :key="rule.id"
          class="rule-item"
        >
          <div class="rule-content">
            <div class="rule-header">
              <h3>{{ rule.name }}</h3>
              <label class="switch">
                <input
                  :checked="rule.enabled"
                  type="checkbox"
                  @change="toggleRule(rule)"
                >
                <span class="slider"></span>
              </label>
            </div>
            <div class="rule-condition">
              触发条件: {{ rule.condition }}
            </div>
            <div class="rule-actions">
              执行动作:
              <span 
                v-for="(action, index) in rule.actions"
                :key="index"
                class="action-tag"
              >
                {{ action }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.scenes-page {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

/* 场景列表样式 */
.scenes-section {
  background-color: var(--bg-light);
  border-radius: 16px;
  padding: 2rem;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

.section-header h2 {
  margin: 0;
  color: var(--text-gray);
}

.add-btn {
  padding: 0.8rem 1.5rem;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.add-btn:hover {
  background-color: var(--secondary-color);
}

.scenes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1.5rem;
}

.scene-card {
  background-color: var(--bg-dark);
  border-radius: 12px;
  padding: 1.5rem;
}

.card-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.scene-icon {
  font-size: 2rem;
}

.scene-info {
  flex: 1;
}

.scene-info h3 {
  margin: 0;
  margin-bottom: 0.3rem;
}

.schedule-tag {
  padding: 0.2rem 0.5rem;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  font-size: 0.8rem;
  color: var(--text-gray);
}

.toggle-btn {
  padding: 0.5rem 1rem;
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-gray);
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.toggle-btn.active {
  background-color: var(--success-color);
  border-color: var(--success-color);
  color: white;
}

.devices-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
}

.device-tag {
  padding: 0.3rem 0.8rem;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  font-size: 0.9rem;
  color: var(--text-gray);
}

.actions-list {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  margin-bottom: 1.5rem;
}

.action-item {
  display: flex;
  justify-content: space-between;
  color: var(--text-gray);
  font-size: 0.9rem;
}

.card-actions {
  display: flex;
  gap: 1rem;
}

.action-btn {
  flex: 1;
  padding: 0.8rem;
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-gray);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-btn.edit:hover {
  border-color: var(--primary-color);
  color: var(--primary-color);
}

.action-btn.delete:hover {
  border-color: var(--error-color);
  color: var(--error-color);
}

/* 定时任务样式 */
.schedules-section {
  background-color: var(--bg-light);
  border-radius: 16px;
  padding: 2rem;
}

.schedules-section h2 {
  margin: 0;
  margin-bottom: 1.5rem;
  color: var(--text-gray);
}

.schedules-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.schedule-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5rem;
  background-color: var(--bg-dark);
  border-radius: 12px;
}

.time {
  font-size: 1.5rem;
  font-weight: 500;
  margin-bottom: 0.5rem;
}

.days {
  color: var(--text-gray);
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.scene-name {
  color: var(--primary-color);
}

/* 联动规则样式 */
.rules-section {
  background-color: var(--bg-light);
  border-radius: 16px;
  padding: 2rem;
}

.rules-section h2 {
  margin: 0;
  margin-bottom: 1.5rem;
  color: var(--text-gray);
}

.rules-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.rule-item {
  background-color: var(--bg-dark);
  border-radius: 12px;
  padding: 1.5rem;
}

.rule-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.rule-header h3 {
  margin: 0;
}

.rule-condition {
  color: var(--text-gray);
  margin-bottom: 1rem;
}

.rule-actions {
  color: var(--text-gray);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.action-tag {
  padding: 0.3rem 0.8rem;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  font-size: 0.9rem;
}

/* 开关样式 */
.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 24px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.1);
  transition: .4s;
  border-radius: 24px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 16px;
  width: 16px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: var(--success-color);
}

input:checked + .slider:before {
  transform: translateX(26px);
}

@media (max-width: 768px) {
  .scenes-grid {
    grid-template-columns: 1fr;
  }

  .card-actions {
    flex-direction: column;
  }

  .schedule-item {
    flex-direction: column;
    gap: 1rem;
    text-align: center;
  }
}
</style>